﻿using System;
using System.Management;
using System.Windows.Forms;

namespace APP.HELPER.Utils.System
{
    public class SystemInfoHelper
    {
        public static bool UseProcessorID;
        public static bool UseBaseBoardProduct;
        public static bool UseBaseBoardManufacturer;
        public static bool UseDiskDriveSignature;
        public static bool UseVideoControllerCaption;
        public static bool UsePhysicalMediaSerialNumber;
        public static bool UseBiosVersion;
        public static bool UseBiosManufacturer;
        public static bool UseWindowsSerialNumber;
        public static string GetSoftware(string Software)
        {
            string text = "";
            int num = 0;
            for (int i = 0; i < Software.Length; i++)
            {
                if (Software[i] == ' ')
                {
                    num = 0;
                }
                if (Software[i] != ' ' & num == 0)
                {
                    text += Software[i].ToString();
                    num = 1;
                }
            }
            return text;
        }
        public static string GetSystemInfo(string SoftwareName)
        {
            if (SystemInfoHelper.UseProcessorID)
            {
                SoftwareName += SystemInfoHelper.RunQuery("Processor", "ProcessorId");
            }
            if (SystemInfoHelper.UseBaseBoardProduct)
            {
                SoftwareName += SystemInfoHelper.RunQuery("BaseBoard", "Product");
            }
            if (SystemInfoHelper.UseBaseBoardManufacturer)
            {
                SoftwareName += SystemInfoHelper.RunQuery("BaseBoard", "Manufacturer");
            }
            if (SystemInfoHelper.UseDiskDriveSignature)
            {
                SoftwareName += SystemInfoHelper.RunQuery("DiskDrive", "Signature");
            }
            if (SystemInfoHelper.UseVideoControllerCaption)
            {
                SoftwareName += SystemInfoHelper.RunQuery("VideoController", "Caption");
            }
            if (SystemInfoHelper.UsePhysicalMediaSerialNumber)
            {
                SoftwareName += SystemInfoHelper.RunQuery("PhysicalMedia", "SerialNumber");
            }
            if (SystemInfoHelper.UseBiosVersion)
            {
                SoftwareName += SystemInfoHelper.RunQuery("BIOS", "Version");
            }
            if (SystemInfoHelper.UseWindowsSerialNumber)
            {
                SoftwareName += SystemInfoHelper.RunQuery("OperatingSystem", "SerialNumber");
            }
            SoftwareName = SystemInfoHelper.RemoveUseLess(SoftwareName);
            return SoftwareName;
        }
        public static void SetDefault()
        {
            SystemInfoHelper.UseBaseBoardManufacturer = false;
            SystemInfoHelper.UseBaseBoardProduct = true;
            SystemInfoHelper.UseBiosManufacturer = true;
            SystemInfoHelper.UseBiosVersion = false;
            SystemInfoHelper.UseDiskDriveSignature = false;
            SystemInfoHelper.UsePhysicalMediaSerialNumber = false;
            SystemInfoHelper.UseProcessorID = true;
            SystemInfoHelper.UseVideoControllerCaption = false;
            SystemInfoHelper.UseWindowsSerialNumber = false;
        }
        public static string GetSystemInfo()
        {
            string text = "";
            if (SystemInfoHelper.UseProcessorID)
            {
                text += SystemInfoHelper.RunQuery("Processor", "ProcessorId");
            }
            if (SystemInfoHelper.UseBaseBoardProduct)
            {
                text += SystemInfoHelper.RunQuery("BaseBoard", "Product");
            }
            if (SystemInfoHelper.UseBaseBoardManufacturer)
            {
                text += SystemInfoHelper.RunQuery("BaseBoard", "Manufacturer");
            }
            if (SystemInfoHelper.UseDiskDriveSignature)
            {
                text += SystemInfoHelper.RunQuery("DiskDrive", "Signature");
            }
            if (SystemInfoHelper.UseVideoControllerCaption)
            {
                text += SystemInfoHelper.RunQuery("VideoController", "Caption");
            }
            if (SystemInfoHelper.UsePhysicalMediaSerialNumber)
            {
                text += SystemInfoHelper.RunQuery("PhysicalMedia", "SerialNumber");
            }
            if (SystemInfoHelper.UseBiosVersion)
            {
                text += SystemInfoHelper.RunQuery("BIOS", "Version");
            }
            if (SystemInfoHelper.UseWindowsSerialNumber)
            {
                text += SystemInfoHelper.RunQuery("OperatingSystem", "SerialNumber");
            }
            text = SystemInfoHelper.RemoveUseLess(text);
            string result;
            if (text.Length < 32)
            {
                result = SystemInfoHelper.GetSystemInfo(text);
            }
            else
            {
                result = text.Substring(0, 32).ToUpper();
            }
            return result;
        }
        private static string RemoveUseLess(string st)
        {
            for (int i = st.Length - 1; i >= 0; i--)
            {
                char c = char.ToUpper(st[i]);
                if ((c < 'A' || c > 'Z') && (c < '0' || c > '9'))
                {
                    st = st.Remove(i, 1);
                }
            }
            return st;
        }
        private static string RunQuery(string TableName, string MethodName)
        {
            ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("Select * from Win32_" + TableName);
            string result;
            using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectSearcher.Get().GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    ManagementObject managementObject = (ManagementObject)enumerator.Current;
                    try
                    {
                        result = managementObject[MethodName].ToString();
                        return result;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            result = "";
            return result;
        }
    }
}
